package com.bensoft.minesweeper;

import com.bensoft.minesweeper.gui.preferences.ApplicationPreferences;
import com.bensoft.minesweeper.resources.FileResources;
import javax.swing.JOptionPane;

/**
 * Manages the game state.
 *
 * @author Ben Wolsieffer
 */
public class GameManager {

    private GameState gameState = GameState.READY;
    private boolean gridReset = true;
    
    public static enum GameState {
        READY,
        RUNNING,
        LOST,
        WON
    }

    GameManager() {
    }

    /**
     * Called when a game is won.
     */
    public void winGame() {
        gameState = GameState.WON;
        ApplicationController.getMinesweeperFrame().getDisplayPanel().gameWon(); // display sun smile
        FileResources.playBing();
        int score = ApplicationController.getMinesweeperFrame().getDisplayPanel().getTime();
        if (isBeginnerMode() && score < ApplicationPreferences.getBeginnerHighScore()) {
            String name = JOptionPane.showInputDialog(ApplicationController.getMinesweeperFrame(), "You have the fastest time for the beginner level. Please enter your name.", "Best Time", JOptionPane.PLAIN_MESSAGE);
            if (name != null && !name.equals("")) {
                ApplicationPreferences.setBeginnerHighScore(score);
                ApplicationPreferences.setBeginnerHighScoreName(name);
            }
        } else if (isIntermediateMode() && score > ApplicationPreferences.getIntermediateHighScore()) {
            String name = JOptionPane.showInputDialog(ApplicationController.getMinesweeperFrame(), "You have the fastest time for the intermediate level. Please enter your name.", "Best Time", JOptionPane.PLAIN_MESSAGE);
            if (name != null && !name.equals("")) {
                ApplicationPreferences.setIntermediateHighScore(score);
                ApplicationPreferences.setIntermediateHighScoreName(name);
            }
        } else if (isAdvancedMode() && score > ApplicationPreferences.getAdvancedHighScore()) {
            String name = JOptionPane.showInputDialog(ApplicationController.getMinesweeperFrame(), "You have the fastest time for the expert level. Please enter your name.", "Best Time", JOptionPane.PLAIN_MESSAGE);
            if (name != null && !name.equals("")) {
                ApplicationPreferences.setAdvancedHighScore(score);
                ApplicationPreferences.setAdvancedHighScoreName(name);
            }
        }
    }

    /**
     * Called when a game is lost.
     */
    public void loseGame() {
        gameState = GameState.LOST;
        ApplicationController.getMinesweeperFrame().getDisplayPanel().gameLost();
        FileResources.playExplosion();
    }

    /**
     * Starts a new game
     */
    public void startGame() {
        if (!gridReset) {
            resetGame();
        }
        ApplicationController.getMinesweeperFrame().getDisplayPanel().startTimer();
        gameState = GameState.RUNNING;
    }

    public void setGridSize(int width, int height, int mines) {
        ApplicationPreferences.setWidth(width);
        ApplicationPreferences.setHeight(height);
        ApplicationPreferences.setMines(mines);
    }

    public void resetGame() {
        gameState = GameState.READY;
        ApplicationController.getMinesweeperFrame().getDisplayPanel().resetTimer();
        ApplicationController.getMinesweeperFrame().getGrid().rebuild();
    }

    /**
     * Returns the current state of the game.
     *
     * @return the game state
     */
    public GameState getGameState() {
        return gameState;
    }

    /**
     * Checks if the game is in beginner mode.
     *
     * @return true if the game is in beginner mode.
     */
    public boolean isBeginnerMode() {
        return ApplicationPreferences.getWidth() == 9 && ApplicationPreferences.getHeight() == 9 && ApplicationPreferences.getMines() == 10;
    }

    /**
     * Checks if the game is in intermediate mode.
     *
     * @return true if the game is in intermediate mode.
     */
    public boolean isIntermediateMode() {
        return ApplicationPreferences.getWidth() == 16 && ApplicationPreferences.getHeight() == 16 && ApplicationPreferences.getMines() == 40;
    }

    /**
     * Checks if the game is in advanced mode.
     *
     * @return true if the game is in advanced mode.
     */
    public boolean isAdvancedMode() {
        return ApplicationPreferences.getWidth() == 30 && ApplicationPreferences.getHeight() == 16 && ApplicationPreferences.getMines() == 99;
    }
}
